package com.net.framework.help.zxing;

import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.net.framework.help.dialog.CustomSimpleDialog;
import com.net.framework.help.utils.StringUtil;
import com.net.framework.help.utils.UIUtils;

/**
 * Author：xiaobin.zhang
 * Email：154722566@qq.com
 * Date：2017/8/1 15:58
 * Desc：<--二维码识别后的跳转类-->
 */
public class QrcodeResultJump implements CustomSimpleDialog.DialogCallback {
    private final static String P_CODE_HEAD = "weixin.qq.com/r/";//公众号二维码地址头部
    private final static String U_CODE_HEAD = "u.wechat.com/";//个人名片二维码地址头部
    private final static String G_CODE_HEAD = "weixin.qq.com/g/";//微信群二维码地址头部
    private final static String DIALOG_CLIP = "clip";
    private Context context;
    private String content;

    public void jump(Context context, String content) {
        this.context = context;
        this.content = content;
        if (StringUtil.isUrl(content)) {
            if (content.contains(P_CODE_HEAD)) {//公众号二维码
//                aa();
                getDialog().getSimpleSingleBtn(null, "关闭", "这是一个微信公众号二维码，请使用微信扫码！", "");
            } else if (content.contains(U_CODE_HEAD)) {//个人名片二维码
                getDialog().getSimpleSingleBtn(null, "关闭", "这是一个微信个人名片二维码，请使用微信扫码！", "");
            } else if (content.contains(G_CODE_HEAD)) {//微信群二维码
                getDialog().getSimpleSingleBtn(null, "关闭", "这是一个微信群二维码，请使用微信扫码！", "");
            } else {//普通url地址，直接跳转
                jumpWeb();
            }
        } else {
            getDialog().getSimpleSingleBtn("二维码内容", "复制二维码内容", content, DIALOG_CLIP);
        }
    }

    private void jumpWeb() {
        Bundle bundle = new Bundle();
        bundle.putString("urlKey", content);
        bundle.putBoolean("isRefresh_Key", false);
        bundle.putBoolean("isCopy_Key", false);

        Intent intent = new Intent();
        //设置Intent的Action属性
        intent.setAction("com.zhiqiyun.woxiao.WEBCUSTOMACTIVITY");
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    /*----------------------弹窗------------------------------*/
    private CustomSimpleDialog customSimpleDialog;

    private CustomSimpleDialog getDialog() {
        if (null == customSimpleDialog) {
            customSimpleDialog = new CustomSimpleDialog(context, this);
        }
        return customSimpleDialog;
    }

    @Override
    public void onDialogButtonOk(Object o) {
        if (DIALOG_CLIP.equals(o.toString())) {
            //复制文章Url地址
            ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            cmb.setText(content);
            UIUtils.shortToast("内容复制成功~");
        }
    }

    private void aa() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("weixin://"));
        ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.plugin.setting.ui.qrcode.GetQRCodeInfoUI");
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setComponent(cmp);
        context.startActivity(intent);


//        Intent i = new Intent(Intent.ACTION_VIEW);
//        i.setData(Uri.parse("weixin://scanqrcode"));
////        i.setData(Uri.parse("weixin://dl/businessWebview/link?url=https://www.baidu.com"));
//        i.setPackage("com.tencent.mm");
//        i.putExtra(Intent.EXTRA_SUBJECT, "Share");//EXTRASUBJECT
//        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//FLAGACTIVITYNEWTASK
//        context.startActivity(i);
    }
}
